<template>
  <div class="xtx-goods-page" v-if="Goods">
    <div class="container">
      <!-- 面包屑 -->
      <Bread>
        <BreadItem to="/">首页</BreadItem>
        <BreadItem :to="'/category/' + Goods.categories[1].id">{{ Goods.categories[1].name }}</BreadItem>
        <BreadItem :to="`/category/sub/${Goods.categories[0].id}`">{{ Goods.categories[0].name }}</BreadItem>
        <BreadItem>{{ Goods.name }}</BreadItem>
      </Bread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="Goods.mainPictures" />
          <GoodsSales :goods="Goods" />
        </div>
        <div class="spec">
          <GoodsName :goods="Goods" />
          <!-- 规格组件 -->
          <GoodsSku :goods="Goods" @change="changeSku" />
          <Numbox label="数量" v-model="num" />
          <Button type="primary" @click="addCart" style="margin-top:20px;">加入购物车</Button>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="Goods.id"></GoodsRelevant>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs />
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜 +专题推荐 -->
        <div class="goods-aside">
          <GoodsHots :goodsId="Goods.id" :type="1" />
          <GoodsHots :goodsId="Goods.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'
import { GetGoods } from "@/api/product"
import { nextTick, provide, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import Numbox from '../../components/library/numbox.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHots from './components/goods-hots.vue'
import GoodsWarn from './components/goods-warn.vue'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
export default {
  name: 'GoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, Numbox, GoodsTabs, GoodsHots, GoodsWarn },
  setup(props) {
    const Goods = useGoods()
    provide('goods', Goods)
    // sku改变时候触发
    const changeSku = (sku) => {
      if (sku.skuId) {
        Goods.value.price = sku.price
        Goods.value.oldPrice = sku.oldPrice
        Goods.value.inventory = sku.inventory
      }
      SKU.value = sku
    }

    // 商品数量
    const num = ref(1)

    // 添加购物车逻辑
    const SKU = ref(null)
    const store = useStore()
    const addCart = () => {
      if (SKU.value) {
        console.log(Goods.value);
        console.log(SKU.value);
        // 本地：id skuId name picture price nowPrice count attrsText selected stock isEffective
        const { id, name, desc: attrsText, inventory: stock, } = Goods.value
        const { skuId, price, specsText } = SKU.value
        store.dispatch('cart/insertCart', {
          id,
          skuId,
          name,
          picture: Goods.value.mainPictures[0],
          price,
          nowPrice: price,
          count: num.value,
          attrsText: specsText,
          stock,
          selected: true,
          isEffective: true
        }).then(() => {
          Message({ type: "success", text: "加入购物车成功" })
        })
      } else {
        Message({ text: '请选择商品规格' })
      }
    }

    return {
      Goods,
      changeSku,
      num,
      addCart
    }
  },
}

// 获取商品详情
const useGoods = () => {
  try {
    const goods = ref(null)
    const route = useRoute()
    watch(() => route.params.id, async (newVal) => {
      if (newVal && `/product/${newVal}` === route.path) {
        const { result } = await GetGoods(route.params.id)
        goods.value = null
        nextTick(() => {
          goods.value = result
        })
      }
    }, { immediate: true })


    return goods

  } catch (error) {
    console.log('获取商品详情', error);
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>